package com.essc.controller;

import com.essc.pojo.Bookmark;
import com.essc.service.BookmarkService;
import com.essc.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
public class BookmarkController {

    @Autowired
    private BookmarkService bookmarkService;

    /*用户查看个人收藏商品*/
    /*需要传入uid pageNum pageSize*/
    @GetMapping("/findBookmark/{uid}/{pageNum}/{pageSize}")
    public JsonResult findBookmark(@PathVariable("uid") Integer uid,
                                   @PathVariable("pageNum") Integer pageNum,
                                   @PathVariable("pageSize") Integer pageSize) {
        return bookmarkService.findBookmarkByUid(uid, pageNum, pageSize);
    }

    /*用户新增收藏商品 在商品详情页面实现*/
    /*需要传入userId goodId */
    @PostMapping("/addBookmark")
    public JsonResult addBookmark(@RequestBody Bookmark bookmark) {
        return bookmarkService.addBookmark(bookmark);
    }

    /*用户将收藏商品移除收藏 在收藏夹实现*/
    /*需要传入userId goodId*/
    @PostMapping("/delBookmark")
    public JsonResult delBookmark(@RequestBody Bookmark bookmark) {
        return bookmarkService.delBookmarkByBid(bookmark);
    }

    /*用户将收藏商品批量移除收藏 在收藏夹实现*/
    /*postman测试需要传入单个单个传bids*/
    @GetMapping("/delBookmarks")
    public JsonResult delBookmarks(@RequestParam String bids) {
        String[] d = bids.split(",");
        return bookmarkService.delBookmarks(d);
    }

    //根据goodid和userid查询是否收藏商品
    @GetMapping("/findBookmark/{uid}/{gid}")
    Boolean findBookmark(@PathVariable("uid") Integer uid,
                         @PathVariable("gid") Integer gid) {
        return bookmarkService.findBookmark(uid, gid);
    }
}
